/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.enterprise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.CompanyBasicInf;
import org.springblade.modules.enterprise.entity.EsmsCry;
import org.springblade.modules.enterprise.entity.EsmsProductCloud;
import org.springblade.modules.enterprise.entity.Problem;
import org.springblade.modules.enterprise.service.ICompanyBasicInfService;
import org.springblade.modules.enterprise.service.IEsmsProductCloudService;
import org.springblade.modules.enterprise.vo.EsmsProductCloudVO;
import org.springblade.modules.enterprise.wrapper.EsmsProductCloudWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 控制器
 *
 * @author BladeX
 * @since 2021-07-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("enterprise/esmsproductcloud")
@Api(value = "", tags = "接口")
public class EsmsProductCloudController extends BladeController {

	private final IEsmsProductCloudService esmsProductCloudService;

	private final ICompanyBasicInfService companyBasicInfService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入esmsProductCloud")
	public R<EsmsProductCloudVO> detail(EsmsProductCloud esmsProductCloud) {
		EsmsProductCloud detail = esmsProductCloudService.getOne(Condition.getQueryWrapper(esmsProductCloud));
		return R.data(EsmsProductCloudWrapper.build().entityVO(detail));
	}
	/**
	 * 详情
	 */
	@GetMapping("/detailForAPP")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入esmsCry")
	public R<EsmsProductCloud> detailForAPP(EsmsProductCloud esmsProductCloud) {
		QueryWrapper<EsmsProductCloud> esmscry=new QueryWrapper<>();
		esmscry.eq("id",esmsProductCloud.getId());
		EsmsProductCloud detail = esmsProductCloudService.getOne(esmscry);
		return R.data(detail);
	}
	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入esmsProductCloud")
	public R<IPage<EsmsProductCloudVO>> list(EsmsProductCloud esmsProductCloud, Query query) {
		IPage<EsmsProductCloud> pages = esmsProductCloudService.page(Condition.getPage(query), Condition.getQueryWrapper(esmsProductCloud));
		return R.data(EsmsProductCloudWrapper.build().pageVO(pages));
	}

	/**
	 * 分页
	 */
	@GetMapping("/unique-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入esmsProductCloud")
	public R<IPage<EsmsProductCloudVO>> uniqueList(EsmsProductCloud esmsProductCloud, Query query) {
		// 获取当前租户ID
		BladeUser user = AuthUtil.getUser();
		esmsProductCloud.setEnterpriseAccount(user.getAccount());
		QueryWrapper<EsmsProductCloud> queryWrapper = Condition.getQueryWrapper(esmsProductCloud);
		queryWrapper.orderByDesc("create_time");
		IPage<EsmsProductCloud> pages = esmsProductCloudService.page(Condition.getPage(query), queryWrapper);
		return R.data(EsmsProductCloudWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入esmsProductCloud")
	public R<IPage<EsmsProductCloudVO>> page(EsmsProductCloudVO esmsProductCloud, Query query) {
		IPage<EsmsProductCloudVO> pages = esmsProductCloudService.selectEsmsProductCloudPage(Condition.getPage(query), esmsProductCloud);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入esmsProductCloud")
	public R save(@Valid @RequestBody EsmsProductCloud esmsProductCloud) {
		// 获取当前租户ID
		BladeUser user = AuthUtil.getUser();
		// 获取企业信息
		CompanyBasicInf detail = companyBasicInfService.getOne(new QueryWrapper<CompanyBasicInf>().eq("tenant_id", user.getTenantId()));
		esmsProductCloud.setCompanyId(detail.getId());
		esmsProductCloud.setCompanyDescription(detail.getCompanyIntroduce());
		esmsProductCloud.setCompanyPhone(detail.getPhone());
		return R.status(esmsProductCloudService.save(esmsProductCloud));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入esmsProductCloud")
	public R update(@Valid @RequestBody EsmsProductCloud esmsProductCloud) {
		return R.status(esmsProductCloudService.updateById(esmsProductCloud));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入esmsProductCloud")
	public R submit(@Valid @RequestBody EsmsProductCloud esmsProductCloud) {
		// 获取当前租户ID
		BladeUser user = AuthUtil.getUser();
		if(esmsProductCloud.getEnterpriseAccount()==null||esmsProductCloud.getEnterpriseAccount()==""){
			esmsProductCloud.setEnterpriseAccount(user.getAccount());
		}
		return R.status(esmsProductCloudService.saveOrUpdate(esmsProductCloud));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(esmsProductCloudService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * 查询状态改变未查阅的数据，并设置为已查询
	 */
	@GetMapping("readService")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "根据服务类型查找对应的子节点")
	public R readService(EsmsProductCloud esmsProductCloud) {
		QueryWrapper<EsmsProductCloud> problemQueryWrapper = new QueryWrapper<>();
		problemQueryWrapper.eq("read_status", 1).eq("enterprise_account",esmsProductCloud.getEnterpriseAccount());
		List<EsmsProductCloud> problemList = esmsProductCloudService.list(problemQueryWrapper);
//		for (EsmsProductCloud ServiceList:problemList) {
//			ServiceList.setReadStatus(2);
//			esmsProductCloudService.saveOrUpdate(ServiceList);
//		}
		return R.data(problemList);
	}
	/**
	 * 查询状态改变未查阅的数据，并设置为已查询
	 */
	@GetMapping("readDetail")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "根据服务类型查找对应的子节点")
	public R readDetail(EsmsProductCloud esmsProductCloud) {
		QueryWrapper<EsmsProductCloud> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",esmsProductCloud.getId());
		EsmsProductCloud detail = esmsProductCloudService.getOne(queryWrapper);
		detail.setReadStatus(2);
		esmsProductCloudService.saveOrUpdate(detail);
		return R.data(detail);

	}

}
